  // 文件路径: src/router/index.ts
  import { createRouter, createWebHistory, RouteLocationNormalized, NavigationGuardNext, RouteRecordRaw } from 'vue-router';
  import Login from '@/views/Login.vue';
  import Dashboard from '@/views/Dashboard.vue';
  import PlantList from '@/views/PlantManage.vue'; // ✅ 路径已确认有效
  
  const routes: RouteRecordRaw[] = [
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: { requiresAuth: false }
    },
    {
      path: '/',
      redirect: '/dashboard'
    },
    {
      path: '/dashboard',
      name: 'Dashboard',
      component: Dashboard,
      meta: { requiresAuth: true }
    },
    {
      path: '/plant',
      name: 'Plant',
      redirect: '/plant/list',
      meta: { requiresAuth: true },
      children: [
        {
          path: 'list',
          name: 'PlantList',
          component: PlantList
        }
      ]
    }
  ];
  
  const router = createRouter({
    history: createWebHistory(),
    routes
  });
  
  // 全局路由守卫
  router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
    const isLoggedIn = localStorage.getItem('token');
  
    if (to.meta.requiresAuth && !isLoggedIn) {
      next('/login');
    } else if (to.path === '/login' && isLoggedIn) {
      next('/dashboard');
    } else {
      next();
    }
  });
  
  export default router;